const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  const { userInfo } = event.data
  const player = {
    openid,
    nickName: userInfo.nickName,
    avatarUrl: userInfo.avatarUrl,
    seat: 0,
    isOnline: true,
    isDrawing: false,
    score: 0
  }
  return db.collection('room').add({
    data: {
      owner: {
        openId: openid,
        nickName: userInfo.nickName,
        avatarUrl: userInfo.avatarUrl
      },
      status: 1,
      players: [player],
      painterIndex: 0,
      updateTime: new Date(),
    }
  })
}